<%@ page pageEncoding="UTF-8" %>
<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css"
          integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/toastr.js/2.1.4/toastr.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-fileinput/5.1.2/css/fileinput.min.css" rel="stylesheet">
    <title>随便打TCS系统</title>
</head>

<body class="nav-md">
<div>
    <div class="container-fluid" style="padding-left: 235px">
        <div>
            <div class="container">
                <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
                    <ol class="carousel-indicators">
<%--                        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>--%>
                    </ol>
                    <div class="carousel-inner">
<%--                        <div class="carousel-item active">--%>
<%--                            <img src="..." style="height: 300px" class="d-block w-100" alt="...">--%>
<%--                        </div>--%>
                    </div>
                    <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
                        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                        <span class="sr-only">Previous</span>
                    </a>
                    <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
                        <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        <span class="sr-only">Next</span>
                    </a>
                </div>
            </div>
            <div id="toolbar" class="btn-group">
                <div class="row">
                    <div class="col-14" style="margin-left: 15px">
                        <form class="form-inline" id="formSelect">
                            <div class="form-group mb-2">
                                <select class="form-control" id="slideStatusSelect">
                                    <option value="" selected:disabled style="diaplay:none">状态</option>
                                    <option value="0">无效</option>
                                    <option value="1">有效</option>
                                </select>
                            </div>
                            <div class="form-group mb-2">
                                <button type="button" id="search" class="btn btn-success">查询</button>
                                <button type="reset" id="reset" class="btn btn-danger">重置</button>
                            </div>
                        </form>
                    </div>
                    <div class="col-14" style="margin-left: 20px">
                        <button type="button" class="btn btn-primary" data-toggle="modal" id="addModal">
                            新增
                        </button>
                    </div>
                </div>
            </div>
            <table id="table"></table>
        </div>
        <!-- 新增 -->
        <div class="modal fade" id="addShow" tabindex="-1" role="dialog" aria-labelledby="addShowTitle">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="addShowTitle">新增首页轮播图</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                    </div>
                    <div class="modal-body">
                        <form id="formAdd">
                            <div class="form-row">
                                <div class="form-group " style="width: 100%">
                                    <input type="text" class="form-control" id="slideNameAdd" placeholder="请输入轮播图名称">
                                </div>
                            </div>
                            <div class="form-row">
                                <input type="text" hidden class="form-control" id="slideSrcSrcAdd"/>
                                <label for="slideSrcAdd">轮播图：</label>
                                <div class="form-group" style="width: 100%;">
                                    <input id="slideSrcAdd" type="file" name="file">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button id="addPost" type="button" class="btn btn-primary">新增</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"
        integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
        crossorigin="anonymous"></script>
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.15.3/locale/bootstrap-table-zh-CN.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-fileinput/5.1.2/js/plugins/purify.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-fileinput/5.1.2/js/fileinput.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-fileinput/5.1.2/js/locales/zh.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-fileinput/5.1.2/js/locales/fa.min.js"></script>
<script>
    $(function () {
        $(document).ajaxComplete(function (event, xhr, settings) {
            let url = xhr.getResponseHeader("redirectUrl");

            let enable = xhr.getResponseHeader("enableRedirect");

            if((enable === "true") && (url !== "")){
                let win = window;
                while(win !== win.top){
                    win = win.top;
                }
                win.location.href = url;
            }
        })
        showSlideshow()
        function showSlideshow(){
            $.ajax({
                url: "/SbdVoip/admin/slideshow/showSlideshow",
                type: "POST",
                data: {},
                dataType: "json",
                contentType: "application/x-www-form-urlencoded",
                success: function (result) {
                    if (result.code !== 2000) {
                        toastr.error(result.message);
                        return
                    }
                    if (result.data === undefined || result.data.length === 0) {
                        return
                    }
                    let carouselIndicators = $(".carousel-indicators")
                    let carouselInner = $(".carousel-inner")
                    let carouselIndicatorsHtml = ""
                    let carouselInnerHtml = ""
                    for (let i = 0; i < result.data.length; i++) {
                        if (i === 0) {
                            carouselIndicatorsHtml += '<li data-target="#carouselExampleIndicators" data-slide-to="'+ i +'" class="active"></li>'
                            carouselInnerHtml += ' <div class="carousel-item active">\n' +
                                '                            <img src="'+ result.data[i].slideSrc + '" ' +
                                'style="height: 300px" class="d-block w-100 rounded-sm border border-success" ' +
                                'alt="'+ result.data[i].slideName +'"></div>'
                        } else {
                            carouselIndicatorsHtml += '<li data-target="#carouselExampleIndicators" data-slide-to="'+ i +'"></li>'
                            carouselInnerHtml += ' <div class="carousel-item">\n' +
                                '                            <img src="'+ result.data[i].slideSrc + '" ' +
                                'style="height: 300px" class="d-block w-100 rounded-sm border border-success" ' +
                                'alt="'+ result.data[i].slideName +'"></div>'
                        }
                        carouselIndicators.html(carouselIndicatorsHtml)
                        carouselInner.html(carouselInnerHtml)
                    }
                }
            });
        }

        Date.prototype.Format = function (fmt) {
            const o = {
                "M+": this.getMonth() + 1,
                "d+": this.getDate(),
                "h+": this.getHours(),
                "m+": this.getMinutes(),
                "s+": this.getSeconds(),
                "q+": Math.floor((this.getMonth() + 3) / 3),
                "S": this.getMilliseconds()
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (let k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };

        window.operateEvents = {
            "click #showDeleteId": function (e, value, row, index) {
                $.ajax({
                    url: "/SbdVoip/admin/slideshow/slideshowWebDel",
                    type: "POST",
                    data: {id: row.id},
                    dataType: "json",
                    contentType: "application/x-www-form-urlencoded",
                    success: function (result) {
                        if (result.code !== 2000) {
                            toastr.error(result.message);
                            return
                        }
                        var table = $('#table');
                        table.bootstrapTable('destroy');
                        table.bootstrapTable(bootstrapTable);
                        toastr.success(result.message);

                        showSlideshow()
                    }
                });
            }
        };

        // 文件上传参数
        const fileInputOption = {
            language: 'zh',
            browseOnZoneClick: true,
            allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg'],
            uploadUrl: '/SbdVoip/layfile/uploadFile1',
            // 是否显示上传按钮
            showUpload: true,
            // 显示移除按钮
            showRemove: false,
            // 是否显示预览
            showPreview: true,
            // 是否显示拖拽区域
            dropZoneEnabled: true,
            // 是否显示文件标题
            showCaption: false,
            minFileCount: 1,
            maxFileCount: 1,
            removeFromPreviewOnError: true,
            layoutTemplates: {
                actionZoom: '<button type="button" class="kv-file-zoom {zoomClass} bi bi-zoom-in" title="{zoomTitle}"><i class="fa fa-search-plus"></i></button>',
                actionDelete: '',
                actionUpload: '',
                actionDrag: '',
                modalMain: '<div id="kvFileinputModal" role="dialog" class="file-zoom-dialog modal fade" tabindex="-1" aria-labelledby="kvFileInputModalLabel"></div>',
                modal: '<div class="modal-dialog modal-lg" role="document">' +
                    '  <div class="modal-content">' +
                    '    <div class="modal-header">' +
                    '      <h3 class="modal-title" id="kvFileInputModalLabel">{heading} <small><span class="kv-zoom-title"></span></small></h3>' +
                    '      <div class="kv-zoom-actions">' +
                    '           <button type="button" class="btn btn-sm btn-kv btn-default btn-outline-secondary btn-toggleheader" title="缩放" data-toggle="button" aria-pressed="false" autocomplete="off">' +
                    '               <i class="fas fa-compress-arrows-alt"></i>' +
                    '           </button>' +
                    '           <button type="button" class="btn btn-sm btn-kv btn-default btn-outline-secondary btn-fullscreen" title="全屏" data-toggle="button" aria-pressed="false" autocomplete="off">' +
                    '               <i class="fas fa-expand"></i>' +
                    '           </button>' +
                    '           <button type="button" class="btn btn-sm btn-kv btn-default btn-outline-secondary btn-borderless" title="无边界模式" data-toggle="button" aria-pressed="true" autocomplete="off">' +
                    '               <i class="fas fa-compress"></i>' +
                    '           </button>' +
                    '           <button type="button" id="close-kv-zoom-actions" class="btn btn-sm btn-kv btn-default btn-outline-secondary" data-dismiss="modal" title="关闭当前预览">' +
                    '               <i class="fas fa-times"></i>' +
                    '           </button>' +
                    '      </div>' +
                    '    </div>' +
                    '    <div class="modal-body">' +
                    '      <div class="floating-buttons"></div>' +
                    '      <div class="kv-zoom-body file-zoom-content"></div>' + '{prev} {next}' +
                    '    </div>' +
                    '  </div>' +
                    '</div>'
            }
        };

        addImg([], fileInputOption, "slideSrcAdd", "slideSrcSrcAdd");

        function addImg(srcArray, fileInputOption, objStr, objSrcStr){
            $("#" + objStr).fileinput(fileInputOption).on('fileuploaded', function (event, data, previewId, index) {
                if (data.response.code !== 0) {
                    toastr.error(data.response.msg);
                    return
                }
                $('#' + objSrcStr).val(data.response.data.src);
                $("[id='" + previewId + "'] .kv-file-content img").attr('src', data.response.data.src);
                toastr.success(data.response.msg);
            });
        }

        $("#kvFileinputModal").on("hidden.bs.modal", function () {
            $("body").attr("class", "modal-open");
        });

        $("#addModal").click(function () {
            $('#formAdd')[0].reset() ;
            $('#addShow').modal('show');
        });

        function actionFormatter(value, row, index) {
            return [
                '<button id="showDeleteId" type="button" class="btn btn-danger">删除</button>'
            ].join('');
        }

        function formatDate(value, row, index) {
            return new Date(parseInt(value)).Format("yyyy-MM-dd hh:mm:ss");
        }

        function slideStatusFormatter(value, row, index) {
            if (value === 0) {
                return '<p class="text-warning">无效</p>';
            }
            if (value === 1) {
                return '<p class="text-success">有效</p>';
            }
        }

        function slideSrcFormatter(value, row, index){
            return '<a target="_blank" href="'+ row.slideSrc +'" class="text-decoration-none">' +
                '<span title="'+ row.slideSrc +'" class="d-inline-block text-truncate" style="max-width: 150px;">'
                + row.slideSrc + '</span></a>'
        }

        let columns = [
            {checkbox: true, 'data-halign': 'center', width: 50},
            {field: 'slideName', title: '轮播图名称', align: 'center', width: 300},
            {field: 'slideSrc', title: '轮播图地址', align: 'center', width: 300,formatter: slideSrcFormatter},
            {field: 'slideStatus', title: '轮播状态', align: 'center', width: 300,formatter: slideStatusFormatter},
            {field: 'createTime', title: '创建时间', align: 'center', width: 272, formatter: formatDate},
            {title: '操作', align: 'center', formatter: actionFormatter, events: operateEvents, width: 500}
        ];

        let bootstrapTable = {
            url: '/SbdVoip/admin/slideshow/slideshowWebList',
            pagination: true,
            toolbar: '#toolbar',
            smartDisplay: false,
            showRefresh: true,
            height: $(window).height() - 300,
            method: 'post',
            pageList: "[10, 15, 20]",
            search: false,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            sidePagination: 'server',
            pageNumber: 1,
            pageSize: 10,
            onlyInfoPagination: false,
            striped: true,
            columns: columns,
            onPostHeader: function () {
                $('#tableId').bootstrapTable('resetView');
            },
            queryParams: function (params) {
                return {
                    offset: (params.offset / params.limit) + 1,
                    limit: params.limit
                };
            },
            responseHandler: function (res) {
                if (res.code !== 2000) {
                    toastr.error(res.message);
                    return {
                        total: 0,
                        rows: []
                    }
                }
                return {
                    total: res.data.total,
                    rows: res.data.rows
                }
            }
        };

        $('#table').bootstrapTable(bootstrapTable);

        $('#search').click(function () {
            let slideStatus = $("#slideStatusSelect").find("option:selected").val();
            bootstrapTable.queryParams = function (params) {
                return {
                    offset: (params.offset / params.limit) + 1,
                    limit: params.limit,
                    slideStatus: slideStatus
                };
            };
            var table = $('#table');
            table.bootstrapTable('destroy');
            table.bootstrapTable(bootstrapTable);
        });

        $("#addPost").click(function () {
            let data = {
                slideName: $('#slideNameAdd').val(),
                slideSrc: $("#slideSrcSrcAdd").val()
            };
            $.ajax({
                url: "/SbdVoip/admin/slideshow/slideshowWebAdd",
                type: "POST",
                data: data,
                dataType: "json",
                contentType: "application/x-www-form-urlencoded",
                success: function (result) {
                    if (result.code !== 2000) {
                        toastr.error(result.message);
                        return
                    }
                    var addShow = $('#addShow');
                    addShow.modal('hide');
                    var table = $('#table');
                    table.bootstrapTable('destroy');
                    table.bootstrapTable(bootstrapTable);
                    toastr.success(result.message);

                    showSlideshow()
                }
            });
        });
    });
</script>
